package com.easpeed.user

import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.EditText
import androidx.core.util.Consumer
import androidx.databinding.DataBindingUtil
import com.bigyu.utils.Logger
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ScreenUtils
import com.easpeed.comon.bean.BaseData
import com.easpeed.comon.bean.Version
import com.easpeed.comon.consts.ConstsOpticalData.options
import com.easpeed.comon.consts.CustomSetting
import com.easpeed.comon.memorycache.CommonMemoryCache
import com.easpeed.comon.utils.ApiHelper.checkVersion
import com.easpeed.comon.utils.DownloadHelper
import com.easpeed.comon.utils.SendCommand
import com.easpeed.comon.utils.SendCommand.commandToStr
import com.easpeed.comon.utils.SocketHelper
import com.easpeed.comon.utils.SocketHelper.sendCommand
import com.easpeed.elevator.R
import com.easpeed.elevator.databinding.UserActivityDeviceInfoBinding
import com.easpeed.elevator.utils.CallbackInterface
import com.easpeed.elevator.utils.NoticeDialog
import com.easpeed.elevator.utils.WwViewHolder
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

class UserDeviceInfoActivity : UserBaseActivity() {

    var binding: UserActivityDeviceInfoBinding? = null

    var holder: WwViewHolder? = null

    override fun initView() {
        ImmersionBar.with(this)
            .statusBarDarkFont(false) // 设置状态栏字体为深色
            .transparentStatusBar()  // 透明状态栏
            .fitsSystemWindows(true)
            .statusBarColor(R.color.colorTitleBar)
            .init()
        binding = DataBindingUtil.setContentView(this, R.layout.user_activity_device_info)
        holder = WwViewHolder(this, this.window.decorView)
        holder!!.setText(R.id.text_version, "v" + AppUtils.getAppVersionName())

        checkVersion(Consumer { versionBaseData: BaseData<Version> ->
            try {
                if (versionBaseData.data!!.version.toInt() > AppUtils.getAppVersionCode()) {
                    holder!!.setText(R.id.update, "(有新版本)")
                    holder!!.setTextColor(R.id.update, Color.GREEN)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        })
        holder!!.setOnClickListener(R.id.layout_version, View.OnClickListener { v: View? ->
            checkVersion(Consumer { versionBaseData: BaseData<Version> ->
                Logger.e("TAG", "checkVersion:$versionBaseData")
                try {
                    if (versionBaseData.data!!.version.toInt() > AppUtils.getAppVersionCode()) {
                        holder!!.setText(R.id.update, "(有新版本)")
                        holder!!.setTextColor(R.id.update, Color.GREEN)
                        DownloadHelper(this).init("更新应用", versionBaseData.data!!.url)
                    }
                } catch (e: java.lang.Exception) {
                    e.printStackTrace()
                }
            })
        })
        binding?.layoutDeviceInfo?.apply {
            val layoutParams = this.layoutParams
            layoutParams.width = CommonMemoryCache.getDialogMaxWidth()
            this.layoutParams = layoutParams
        }

        binding?.back?.setOnClickListener {
            finish()
        }


        holder!!.setOnClickListener(R.id.back, View.OnClickListener { v: View? ->
            finish()
        })

        holder!!.setOnClickListener(R.id.layout_chanel_1, View.OnClickListener { v: View? ->
            modifyChanel(true, "编辑设备通道1折射率", holder!!.getText(R.id.text_chanel_1), object : CallbackInterface<String> {
                override fun onCallback(message: String) {
                    holder!!.setText(R.id.text_chanel_1, message)
                    editor.putString("chanel_1", message)
                    editor.commit()
                }
            })
        })
        holder!!.setOnClickListener(R.id.layout_chanel_2, View.OnClickListener { v: View? ->
            modifyChanel(true, "编辑设备通道2折射率", holder!!.getText(R.id.text_chanel_2), object : CallbackInterface<String> {
                override fun onCallback(message: String) {
                    holder!!.setText(R.id.text_chanel_2, message)
                    editor.putString("chanel_2", message)
                    editor.commit()
                }
            })
        })
        holder!!.setOnClickListener(R.id.layout_chanel_3, View.OnClickListener { v: View? ->
            modifyChanel(true, "编辑设备通道3折射率", holder!!.getText(R.id.text_chanel_3), object : CallbackInterface<String> {
                override fun onCallback(message: String) {
                    holder!!.setText(R.id.text_chanel_3, message)
                    editor.putString("chanel_3", message)
                    editor.commit()
                }
            })
        })
        holder!!.setOnClickListener(R.id.layout_chanel_4, View.OnClickListener { v: View? ->
            modifyChanel(true, "编辑设备通道4折射率", holder!!.getText(R.id.text_chanel_4), object : CallbackInterface<String> {
                override fun onCallback(message: String) {
                    holder!!.setText(R.id.text_chanel_4, message)
                    editor.putString("chanel_4", message)
                    editor.commit()
                }
            })
        })
        holder!!.setOnClickListener(R.id.layout_device_level, View.OnClickListener { v: View? ->
            selectLevel("选择设备级别", holder!!.getText(R.id.text_device_level), object : CallbackInterface<String> {
                override fun onCallback(message: String) {
                    holder!!.setText(R.id.text_device_level, message)
                    editor.putString("deviceLevel", message)
                    editor.commit()
                }
            })
        })
        holder!!.setOnClickListener(R.id.layout_device_model, View.OnClickListener { v: View? ->
            modifyChanel(false, "编辑设备型号", holder!!.getText(R.id.text_device_model), object : CallbackInterface<String> {
                override fun onCallback(message: String) {
                    holder!!.setText(R.id.text_device_model, message)
                    CommonMemoryCache.deviceModel = message
                    editor.putString("deviceModel", message)
                    editor.commit()
                }
            })
        })
        holder!!.setOnClickListener(R.id.layout_device_name, View.OnClickListener { v: View? ->
            modifyChanel(false, "编辑设备名称", holder!!.getText(R.id.text_device_name), object : CallbackInterface<String> {
                override fun onCallback(message: String) {
                    holder!!.setText(R.id.text_device_name, message)
                    CommonMemoryCache.deviceName = message
                    editor.putString("deviceName", message)
                    editor.commit()

                    SocketHelper.sendUpdateName()
                }
            })
        })


        val chanel_1: String = sp.getString("chanel_1", "1.467").toString()
        val chanel_2: String = sp.getString("chanel_2", "1.467").toString()
        val chanel_3: String = sp.getString("chanel_3", "1.467").toString()
        val chanel_4: String = sp.getString("chanel_4", "1.467").toString()

        holder!!.setText(R.id.text_chanel_1, chanel_1)
        holder!!.setText(R.id.text_chanel_2, chanel_2)
        holder!!.setText(R.id.text_chanel_3, chanel_3)
        holder!!.setText(R.id.text_chanel_4, chanel_4)

        holder!!.setText(R.id.text_device_name, sp.getString("deviceName", "XXX光缆设备"))
        holder!!.setText(R.id.text_device_model, sp.getString("deviceModel", "XXX型号"))
        holder!!.setText(R.id.text_device_level, sp.getString("deviceLevel", "一级"))

        holder!!.setOnClickListener(R.id.layout_otdr_setting, View.OnClickListener { v: View? ->
            modifyOTDR()
        })
        holder!!.setOnClickListener(R.id.layout_dvs_setting, View.OnClickListener { v: View? ->
            modifyDvs()
        })
    }

    private fun modifyDvs() {
        val resID = R.layout.dialog_dvs_setting
        val dialog = NoticeDialog.Builder(holder!!.mContext).setLayoutId(resID).setClickBtnDismissDialog(true).setCanceledOnTouchOutside(false).create()
        dialog.setDialogWith(0, ScreenUtils.getScreenHeight())
        dialog.setGravity(Gravity.CENTER)
        dialog.show()
        if (!CustomSetting.use8ChannelData) {
            dialog.setVisibility(R.id.layout_channel5, View.GONE)
            dialog.setVisibility(R.id.layout_channel6, View.GONE)
            dialog.setVisibility(R.id.layout_channel7, View.GONE)
            dialog.setVisibility(R.id.layout_channel8, View.GONE)
        }
        dialog.setOnclickListener(R.id.sure, View.OnClickListener { v: View? ->
            val change1 = dialog.getView(R.id.input1) as EditText
            val change2 = dialog.getView(R.id.input2) as EditText
            val change3 = dialog.getView(R.id.input3) as EditText
            val change4 = dialog.getView(R.id.input4) as EditText
            val change5 = dialog.getView(R.id.input51) as EditText
            val  change6 = dialog.getView(R.id.input6) as EditText
            val  change7 = dialog.getView(R.id.input7) as EditText
            val  change8 = dialog.getView(R.id.input8) as EditText
            val pdjl = dialog.getView(R.id.input5) as EditText

            editor.putString("chanel_1_dvs", change1.text.toString())
            editor.putString("chanel_2_dvs", change2.text.toString())
            editor.putString("chanel_3_dvs", change3.text.toString())
            editor.putString("chanel_4_dvs", change4.text.toString())
            editor.putString("chanel_5_dvs", change5.text.toString())
            editor.putString("chanel_6_dvs", change6.text.toString())
            editor.putString("chanel_7_dvs", change7.text.toString())
            editor.putString("chanel_8_dvs", change8.text.toString())
            editor.putString("pdjl", pdjl.text.toString())
            editor.commit()

            val map = HashMap<String, String>()
            map["chanel_1"] = change1.text.toString()
            map["chanel_2"] = change2.text.toString()
            map["chanel_3"] = change3.text.toString()
            map["chanel_4"] = change4.text.toString()
            map["pdjl"] = pdjl.text.toString()
            sendCommand(commandToStr(SendCommand.commandSyncDVSSetting), Gson().toJson(map))
        })
        val chanel_1 = sp.getString("chanel_1_dvs", "1.467")
        val chanel_2 = sp.getString("chanel_2_dvs", "1.467")
        val chanel_3 = sp.getString("chanel_3_dvs", "1.467")
        val chanel_4 = sp.getString("chanel_4_dvs", "1.467")
        val chanel_5 = sp.getString("chanel_5_dvs", "1.467")
        val chanel_6 = sp.getString("chanel_6_dvs", "1.467")
        val chanel_7 = sp.getString("chanel_7_dvs", "1.467")
        val chanel_8 = sp.getString("chanel_8_dvs", "1.467")
        val pdjl = sp.getString("pdjl", "70")

        dialog.setText(R.id.input1, chanel_1)
        dialog.setText(R.id.input2, chanel_2)
        dialog.setText(R.id.input3, chanel_3)
        dialog.setText(R.id.input4, chanel_4)
        dialog.setText(R.id.input51, chanel_5)
        dialog.setText(R.id.input6, chanel_6)
        dialog.setText(R.id.input7, chanel_7)
        dialog.setText(R.id.input8, chanel_8)
        dialog.setText(R.id.input5, pdjl)
        dialog.setOnclickListener(R.id.cancel, View.OnClickListener { v: View? -> })
    }

    private fun modifyOTDR() {
        val resID = R.layout.dialog_otdr_setting
        val dialog = NoticeDialog.Builder(holder!!.mContext).setLayoutId(resID).setClickBtnDismissDialog(true).setCanceledOnTouchOutside(false).create()
        dialog.setDialogWith(0, ScreenUtils.getScreenHeight())
        dialog.setGravity(Gravity.CENTER)
        dialog.show()
        if (!CustomSetting.use8ChannelData) {
            dialog.setVisibility(R.id.layout_channel5, View.GONE)
            dialog.setVisibility(R.id.layout_channel6, View.GONE)
            dialog.setVisibility(R.id.layout_channel7, View.GONE)
            dialog.setVisibility(R.id.layout_channel8, View.GONE)
        }
        dialog.setOnclickListener(R.id.sure, View.OnClickListener { v: View? ->
            val change1 = dialog.getView(R.id.input1) as EditText
            val change2 = dialog.getView(R.id.input2) as EditText
            val change3 = dialog.getView(R.id.input3) as EditText
            val change4 = dialog.getView(R.id.input4) as EditText
            val change5 = dialog.getView(R.id.input5) as EditText
            val  change6 = dialog.getView(R.id.input6) as EditText
            val  change7 = dialog.getView(R.id.input7) as EditText
            val  change8 = dialog.getView(R.id.input8) as EditText

            editor.putString("chanel_1_otdr", change1.text.toString())
            editor.putString("chanel_2_otdr", change2.text.toString())
            editor.putString("chanel_3_otdr", change3.text.toString())
            editor.putString("chanel_4_otdr", change4.text.toString())
            editor.putString("chanel_5_otdr", change5.text.toString())
            editor.putString("chanel_6_otdr", change6.text.toString())
            editor.putString("chanel_7_otdr", change7.text.toString())
            editor.putString("chanel_8_otdr", change8.text.toString())
            editor.commit()

            val map = HashMap<String, String>()
            map["chanel_1"] = change1.text.toString()
            map["chanel_2"] = change2.text.toString()
            map["chanel_3"] = change3.text.toString()
            map["chanel_4"] = change4.text.toString()
            map["chanel_5"] = change5.text.toString()
            map["chanel_6"] = change6.text.toString()
            map["chanel_7"] = change7.text.toString()
            map["chanel_8"] = change8.text.toString()
            sendCommand(commandToStr(SendCommand.commandSyncOTDRSetting), Gson().toJson(map))
        })
        val chanel_1 = sp.getString("chanel_1_otdr", "1.467")
        val chanel_2 = sp.getString("chanel_2_otdr", "1.467")
        val chanel_3 = sp.getString("chanel_3_otdr", "1.467")
        val chanel_4 = sp.getString("chanel_4_otdr", "1.467")
        val chanel_5 = sp.getString("chanel_5_otdr", "1.467")
        val chanel_6 = sp.getString("chanel_6_otdr", "1.467")
        val chanel_7 = sp.getString("chanel_7_otdr", "1.467")
        val chanel_8 = sp.getString("chanel_8_otdr", "1.467")

        dialog.setText(R.id.input1, chanel_1)
        dialog.setText(R.id.input2, chanel_2)
        dialog.setText(R.id.input3, chanel_3)
        dialog.setText(R.id.input4, chanel_4)
        dialog.setText(R.id.input5, chanel_5)
        dialog.setText(R.id.input6, chanel_6)
        dialog.setText(R.id.input7, chanel_7)
        dialog.setText(R.id.input8, chanel_8)
        dialog.setOnclickListener(R.id.cancel, View.OnClickListener { v: View? -> })
    }
    private fun modifyChanel(isNumber: Boolean, title: String, chanel: String, callbackInterface: CallbackInterface<String>) {
        var resID = R.layout.dialog_input
        if (isNumber) {
            resID = R.layout.dialog_input_number
        }
        val dialog = NoticeDialog.Builder(holder!!.mContext).setLayoutId(resID).setClickBtnDismissDialog(true).setCanceledOnTouchOutside(false).create()

        dialog.setDialogWith(CommonMemoryCache.getDialogMaxWidth(), ScreenUtils.getScreenHeight())
        dialog.setGravity(Gravity.CENTER)
        dialog.show()

        dialog.setText(R.id.title, title)
        dialog.setText(R.id.input, chanel)
        dialog.setOnclickListener(R.id.sure, View.OnClickListener { v: View? ->
            val text = dialog.getView(R.id.input) as EditText
            callbackInterface.onCallback(text.text.toString())
        })
        dialog.setOnclickListener(R.id.cancel, View.OnClickListener { v: View? -> })
    }


    private fun selectLevel(title: String, chanel: String, callbackInterface: CallbackInterface<String>) {
        val resID = R.layout.dialog_input_autoinput
        val dialog = NoticeDialog.Builder(holder!!.mContext).setLayoutId(resID).setClickBtnDismissDialog(true).setCanceledOnTouchOutside(false).create()

        dialog.setDialogWith(CommonMemoryCache.getDialogMaxWidth(), ScreenUtils.getScreenHeight())
        dialog.setGravity(Gravity.CENTER)
        dialog.show()

        dialog.setText(R.id.title, title)
        dialog.setText(R.id.input, chanel)
        dialog.setOnclickListener(R.id.sure, View.OnClickListener { v: View? ->
            val text = dialog.getView(R.id.input) as EditText
            callbackInterface.onCallback(text.text.toString())
        })


        val autoCompleteTextView = dialog.getView(R.id.input) as AutoCompleteTextView
        // 准备下拉选项数据
        val adapter = ArrayAdapter(holder!!.mContext, android.R.layout.simple_dropdown_item_1line, options)
        autoCompleteTextView.setAdapter(adapter)
        autoCompleteTextView.setOnClickListener(View.OnClickListener { v: View? -> autoCompleteTextView.showDropDown() })

        dialog.setOnclickListener(R.id.cancel, View.OnClickListener { v: View? -> })
    }
}